<template>
  <div id="labCompanyFeeitemDiv">
    <Splitpanes>
      <Pane size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <vxe-input style="width: 180px" v-model="searchCompanyFeeForm.seachValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="findCompanyUserList" @keyup="keyupCompanyFeeEnter" @search-click="findCompanyUserList()"></vxe-input>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="ploading" @click="findCompanyUserList"></vxe-button>
            <vxe-button type="text" icon="vxe-icon-delete" class="button-red" content="删除" @click="deleteBatchCompanyUser"></vxe-button>
            <!-- <div class="msgDiv">双击移除</div> -->
          </template>
        </vxe-toolbar>
        <div class="rightTable">
          <!--@cell-dblclick="tatItemRowClick"   双击移除-->
          <vxe-table ref="tatItemTableRef" class="mytable-scrollbar supplierTableClazz" border="inner" align="center" height="auto" :data="companyUserList" :loading="ploading" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" :edit-config="{ trigger: 'click', mode: 'row', showStatus: true }" resizable show-overflow keep-source>
            <vxe-column type="checkbox" width="30"></vxe-column>
            <vxe-column field="username" title="用户编码" width="90"></vxe-column>
            <vxe-column field="realname" title="用户名称" min-width="180"></vxe-column>
          </vxe-table>
        </div>
      </Pane>
      <Pane size="50">
        <vxe-toolbar perfect>
          <template #buttons>
            <vxe-input style="width: 180px" v-model="searchForm.seachValue" type="search" clearable class="vxeInputSearch" placeholder="关键字查询" @clear="findUserList" @keyup="keyupEnter" @search-click="findUserList()"></vxe-input>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-refresh" content="刷新" :loading="floading" @click="findUserList"></vxe-button>
          </template>
        </vxe-toolbar>
        <div class="rightTable">
          <vxe-table ref="feeItemTableRef" class="mytable-scrollbar supplierTableClazz" border="inner" align="center" height="auto" :data="userList" :loading="floading" @cell-dblclick="userRowClick" :row-config="{ isCurrent: true, isHover: true }" :column-config="{ resizable: true }" resizable show-overflow keep-source>
            <vxe-column type="seq" title="#" width="30"></vxe-column>
            <vxe-column field="username" title="用户编码" width="90"></vxe-column>
            <vxe-column field="realname" title="用户名称" min-width="180"></vxe-column>
          </vxe-table>
        </div>

      </Pane>
    </Splitpanes>
  </div>
</template>
<script setup lang="ts">
// 引入组件区 ==========
import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
import Breadcrumb from '@/components/Breadcrumb.vue';
import { Splitpanes, Pane } from 'splitpanes';
import moment from 'moment';
let { proxy }: any = getCurrentInstance();
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage'; //提示
import { isEmpty } from 'lodash';
defineExpose({ initPage });
let inspectCompany: any = reactive({});
let floading = ref(false);
let ploading = ref(false);

let userList = reactive([]);
let companyUserList = reactive([]);

let searchForm = reactive({
  seachValue: ''
});
let searchCompanyFeeForm = {
  seachValue: ''
};
function initPage(row: any) {
  inspectCompany = row;
  findCompanyUserList();
  findUserList();
}

function keyupCompanyFeeEnter(e: any) {
  if (e.$event.code == 'Enter') {
    findCompanyUserList();
  }
}
function findCompanyUserList() {
  ploading.value = true;
  companyUserList.length = 0; //放入数据前清空数据
  const url = '/lab/inspectCompanyUser/findInspectCompanyUserList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        seachValue: searchCompanyFeeForm.seachValue,
        companyCode: inspectCompany.companyCode
      }
    })
    .then((res: any) => {
      companyUserList.push(...res.data);
      ploading.value = false;
    })
    .catch((err: any) => {
      ploading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function keyupEnter(e: any) {
  if (e.$event.code == 'Enter') {
    findUserList();
  }
}

function findUserList() {
  floading.value = true;
  userList.length = 0; //放入数据前清空数据
  const url = '/api/user/getUserInfoList';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        keywords: searchForm.seachValue,
        companyCode: inspectCompany.companyCode
      }
    })
    .then((res: any) => {
      userList.push(...res.data);

      floading.value = false;
    })
    .catch((err: any) => {
      floading.value = false;
      // VxeMessage('获取获取当前字典的规则列表失败',VxeNotifyType.error)
    });
}

function userRowClick({ row }: any) {
  if (isEmpty(inspectCompany)) {
    VxeMessage('请选择送检单位', VxeNotifyType.error);
    return;
  }
  const url = '/lab/inspectCompanyUser/addCompanyUser';
  let purposeItem = {
    companyId: inspectCompany.id,
    companyCode: inspectCompany.companyCode,
    username: row.username,
    realname: row.realname
  };
  proxy.$http
    .post(url, purposeItem)
    .then((res: any) => {
      VxeMessage('成功', VxeNotifyType.success);
      findCompanyUserList();
      findUserList();
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}

function deleteBatchCompanyUser() {
  const sltTatItems = (proxy.$refs.tatItemTableRef as any).getCheckboxRecords();
  if (!(sltTatItems.length > 0)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.info);
  } else {
    proxy
      .$confirm('确定要删除当前勾选的数据？', '提示', {
        cancelButtonClass: 'cancel-button',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        const url = '/lab/inspectCompanyUser/batchDeleteCompanyUserList';
        proxy.$http
          .post(url, sltTatItems)
          .then((res: any) => {
            if (res.status.code === 1) {
              findCompanyUserList();
              findUserList();
              VxeMessage('删除成功', VxeNotifyType.success);
            } else {
              VxeMessage(res.status.msg, VxeNotifyType.error);
            }
          })
          .catch((err: any) => {
            VxeMessage(err, VxeNotifyType.error);
          });
      })
      .catch((err: any) => {
        // VxeMessage(err, VxeNotifyType.error);
      });
  }
}
</script>

<style lang="scss" scoped>
#labCompanyFeeitemDiv {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }

  //右侧质控项目表格
  .tableBox {
    height: calc(100% - 40px);
    background-color: $base-white;
  }

  .rightTable {
    height: calc(100% - 40px);
  }
}
</style>